﻿using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.DbContext.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Windows;
using Unity;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class ProjectReportViewModel : RegionViewModelBase
    {
        private IEnumerable<ReportKey> _tagsBySpec;
        private IEnumerable<ReportKey> _tagsByType;

        private Point _panOffset;
        private Size _zoom;
        private double _gapLength = 0.5d;
        private double _axisMaxValue = 20000d;
        private double _axisStep = 50d;
        private string _axisTitle = "PROFIT (USD)";
        private string _axisLabelFormat = "N0";

        private readonly IProjectReportService _projectReport;
        public ProjectReportViewModel(IProjectReportService projectReport, IRegionManager regionManager, IUnityContainer unityContainer, IDialogService dialogService)
            : base(regionManager, unityContainer, dialogService)
        {
            _projectReport = projectReport;
            this.Zoom = new Size(15, 1);
            this.PanOffset = new Point(0, 0);
        }

        protected override async void OnInitPage(object obj)
        {
            try
            {
                var result = await _projectReport.GetStatisticsReportAsync(GlobalObject.CurrentProject.ProjId.ToInt());
                if (result == null)
                {
                    return;
                }
                TagsBySpec = result.TagsBySpec;
                TagsByType = result.TagsByType;

                var maxSpec = TagsBySpec.MaxBy(k => k.Count);
                var maxType = TagsByType.MaxBy(k => k.Count);
                AxisMaxValue = maxType.Count + 50;

                var step = Math.Round((AxisMaxValue / 20) / 10, 1);
                AxisStep = step * 10;
            }
            catch (Exception x)
            {
                var msg = $"页面加载数据失败：" + x.Message;
                LoggerHelper.Current.Error(msg);
                Dialog.ShowAltert(msg);
            }
        }

        public IEnumerable<ReportKey> TagsBySpec
        {
            get
            {
                return this._tagsBySpec;
            }
            set
            {
                if (this._tagsBySpec != value)
                {
                    this._tagsBySpec = value;
                    this.OnPropertyChanged(nameof(TagsBySpec));
                }
            }
        }

        public IEnumerable<ReportKey> TagsByType
        {
            get
            {
                return this._tagsByType;
            }
            set
            {
                if (this._tagsByType != value)
                {
                    this._tagsByType = value;
                    this.OnPropertyChanged(nameof(TagsByType));
                }
            }
        }

        public double GapLength
        {
            get
            {
                return this._gapLength;
            }
            set
            {
                if (this._gapLength != value)
                {
                    this._gapLength = value;
                    this.OnPropertyChanged(nameof(GapLength));
                }
            }
        }

        public double AxisMaxValue
        {
            get
            {
                return this._axisMaxValue;
            }
            set
            {
                if (this._axisMaxValue != value)
                {
                    this._axisMaxValue = value;
                    this.OnPropertyChanged(nameof(AxisMaxValue));
                }
            }
        }

        public double AxisStep
        {
            get
            {
                return this._axisStep;
            }
            set
            {
                if (this._axisStep != value)
                {
                    this._axisStep = value;
                    this.OnPropertyChanged(nameof(AxisStep));
                }
            }
        }

        public string AxisTitle
        {
            get
            {
                return this._axisTitle;
            }
            set
            {
                if (this._axisTitle != value)
                {
                    this._axisTitle = value;
                    this.OnPropertyChanged(nameof(AxisTitle));
                }
            }
        }

        public string AxisLabelFormat
        {
            get
            {
                return this._axisLabelFormat;
            }
            set
            {
                if (this._axisLabelFormat != value)
                {
                    this._axisLabelFormat = value;
                    this.OnPropertyChanged(nameof(AxisLabelFormat));
                }
            }
        }

        public Point PanOffset
        {
            get
            {
                return _panOffset;
            }
            set
            {
                if (this._panOffset != value)
                {
                    this._panOffset = value;
                    this.OnPropertyChanged(nameof(PanOffset));
                }
            }
        }


        public Size Zoom
        {
            get
            {
                return _zoom;
            }
            set
            {
                if (this._zoom != value)
                {
                    this._zoom = value;
                    this.OnPropertyChanged(nameof(Zoom));
                }
            }
        }
    }
}
